package com.hjq.fcloud.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.hjq.fcloud.BuildConfig;
import com.hjq.fcloud.R;
import com.hjq.fcloud.callback.DialogCallBack;
import com.hjq.fcloud.http.Urls;
import com.hjq.fcloud.ui.activity.BrowserActivity;


public class RightAlertDialog extends Dialog {
    public static final int CONFIRM = 1;
    public static final int CANCELBALANCE = 2;
    private static final int COUNT_DOWN = 1001;
    private String string_permission_private;
    private String string_permission_user;
    private DialogCallBack callBack;
    private TextView alert_title;
    private TextView msg;
    private TextView cancel;
    private TextView ok;
    private Activity context;

    public RightAlertDialog(Activity context, int theme, DialogCallBack callBack) {
        super(context, theme);
        this.context = context;
        this.callBack = callBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        string_permission_private = getContext().getResources().getString(R.string.string_permission_private);
        string_permission_user = getContext().getResources().getString(R.string.string_permission_user);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.right_alert_dialog);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        alert_title = (TextView) findViewById(R.id.alert_title);
        alert_title.setText(string_permission_user + "和" + string_permission_private);
        msg = (TextView) findViewById(R.id.alert_msg);
        cancel = findViewById(R.id.alert_btn_cancel);
        ok = findViewById(R.id.alert_btn_ok);
        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (callBack != null)
                    callBack.callBack(CANCELBALANCE, "ok");
                RightAlertDialog.this.dismiss();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.callBack(CONFIRM, "ok");
                RightAlertDialog.this.dismiss();
            }
        });

        setContent();
    }

    private void setContent() {
        //msg
        String content = "欢迎使用“工程宝”！我们非常重视您的个人信息和隐私保护。在您使用我们提供的服务之前，请务必审慎阅读、充分理解《" + string_permission_user + "》和《" + string_permission_private + "》各项条款，我们将严格按照经您同意的各项条款使用您的个人信息，以便为您提供更好的服务。\n" +
                "\n" +
                "您如果同意，请点击“ 我同意”并开始使用我们的产品和服务，我们会尽全力保护您的个人信息安全。";
        int first_start = content.indexOf("《");
        int first_end = content.indexOf("》") + 1;
        int second_start = content.indexOf("《", first_start + 1);
        int second_end = content.indexOf("》", first_end + 1) + 1;
        SpannableString spStr = new SpannableString(content);
        spStr.setSpan(new NoLineClickSpan(content.substring(first_start + 1, first_end - 1)), first_start, first_end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spStr.setSpan(new NoLineClickSpan(content.substring(second_start + 1, second_end - 1)), second_start, second_end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        msg.setText(spStr);
        msg.setMovementMethod(LinkMovementMethod.getInstance());
    }


    private class NoLineClickSpan extends ClickableSpan {
        String text;

        public NoLineClickSpan(String text) {
            super();
            this.text = text;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(getContext().getResources().getColor(R.color.common_color_blue));
            ds.setUnderlineText(false); //去掉下划线
        }

        @Override
        public void onClick(View widget) {
            msg.invalidate();
            processHyperLinkClick(context, text.contains(string_permission_private)); //点击超链接时调用
        }
    }

    public static void processHyperLinkClick(Activity activity,boolean isPrivate) {
        if (isPrivate) {
            BrowserActivity.Companion.start(activity, BuildConfig.HOST_URL + Urls.URL_PRIVACY_POLICY);
        } else {
            BrowserActivity.Companion.start(activity, BuildConfig.HOST_URL + Urls.URL_USER_AGREEMENT);
        }
    }

    public void setAnim(int animID) {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        // 设置动画
        dialogWindow.setWindowAnimations(animID);
        dialogWindow.setAttributes(lp);
    }
}
